#include "AppChooser.h"
#include <QVBoxLayout>
#include <QListWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QKeyEvent>
#include "Config.h"
#include "Utils.h"
#include "FileNode.h"
/*---------------------------------------------------------------------------*/
AppChooser::AppChooser(vector<FileNode*> vfn)
{
	QVBoxLayout *mainLayout = new QVBoxLayout();
	list = new QListWidget();
	
	// List
	vector<string> prelist;
	
	
	foreach( Option*, o, *Config::getOptions() )
	{
		Option *option = *o;
		
		for(unsigned int i=0; i<option->count; i++)
		{
			string value = option->values[i];

			bool need = true;
		
			foreach( string, str, prelist )
			{
			
				if( !(*str).compare( value ) )
				{
					need = false;	
				} 
			}	
		
			if( need )
			{
				prelist.push_back( value );
				list->addItem( QString( value.c_str() ) );
			}
	
		}

	}
	
	list->sortItems();
	
	// Layouts and widgets
	text = new QLineEdit("");
	QPushButton *button = new QPushButton("Open");
	mainLayout->addWidget( new QLabel("Select the launcher : ") );
	mainLayout->addWidget( list );
	mainLayout->addWidget( text );
	mainLayout->addWidget( button );
	mainLayout->setSizeConstraint( QLayout::SetFixedSize );
	
	setLayout(mainLayout);
	setWindowTitle(tr("gfm : Open with ..."));
	resize(400,300);
	setVisible(true);
	setAttribute(Qt::WA_DeleteOnClose, true);
	setGeometry( QCursor::pos().x() , QCursor::pos().y(), 400, 300 );  
	text->setFocus( Qt::MouseFocusReason );


	// Connections
	connect( list, SIGNAL( itemSelectionChanged() ), this, SLOT( selectedChanged() ));
	connect( list, SIGNAL( itemActivated(QListWidgetItem*) ), this, SLOT( openClicked() ));
	connect( button, SIGNAL( clicked() ), this, SLOT( openClicked() ));
	connect( text, SIGNAL( returnPressed() ), this, SLOT( openClicked() ));
	connect( text, SIGNAL( textEdited( const QString & ) ), this, SLOT( textChanged() ));
	
	connect( text, SIGNAL( cursorPositionChanged( int, int ) ), this, SLOT( cursorPositionChanged( int, int ) ));


	// Properties
	v_files = vfn;
	oldcurpos = 0;
}
/*---------------------------------------------------------------------------*/	
AppChooser::~AppChooser()
{

	DEBUGMSG("AppChooser::~AppChooser() destroyed");
}
/*---------------------------------------------------------------------------*/	
void AppChooser::selectedChanged(  )
{
	QListWidgetItem *item = list->item( list->currentRow() );
	text->setText( item->data(Qt::DisplayRole).toString() );
}	
/*---------------------------------------------------------------------------*/	
void AppChooser::selectedChanged( QListWidgetItem *item )
{
	text->setText( item->data(Qt::DisplayRole).toString() );
}
/*---------------------------------------------------------------------------*/	
void AppChooser::textChanged()
{
	DEBUGMSG( "AppChooser::textChanged()" );
	
	QString str = text->text();
	
	str = str.left( text->cursorPosition() );


	if( str.length() > 0 )
	{	
		QList<QListWidgetItem*> matches = list->findItems( str, Qt::MatchStartsWith );
		if( matches.count() > 0 )
		{
			QString appfound = matches[0]->data(Qt::DisplayRole).toString();			
			int curpos = text->cursorPosition();			
			text->setText(appfound);
			text->setSelection( curpos, appfound.length() - curpos );			
		}
	}

}
/*---------------------------------------------------------------------------*/	
void AppChooser::openClicked()
{
	launch();
}
/*---------------------------------------------------------------------------*/	
void AppChooser::launch()
{
	string cmd, appname, params, workdir;
	
	cmd = "cd ";
	appname = text->text().toStdString();
	
	if( !appname.compare("") ) 
	{
		close();
		return;
	}
	
	params = "";
	foreach( FileNode*, fn, v_files )
	{
		params += Utils::escapeChars( (*fn)->getAbsolutePath() );
		params += " ";
		workdir = Utils::escapeChars( (*fn)->getParent()->getAbsolutePath() );
	}
	
	cmd += workdir + "; " + appname + " " + params + " &";
	system(cmd.c_str());
	DEBUGMSG("AppChooser::launch() : " << cmd);
	

	close();
}
/*---------------------------------------------------------------------------*/	
void AppChooser::cursorPositionChanged ( int old, int nu )
{
	if( nu < old ) 
	{
		DEBUGMSG("oui");
	}
}
/*---------------------------------------------------------------------------*/	
